import { _decorator, Color, Component, Graphics, log, Node, UITransform, Vec2, Vec3, tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('LineScript')
export class LineScript extends Component {

    public ctx: Graphics

    @property(Node)
    bow: Node

    start() {
        this.ctx = this.node.getComponent(Graphics);
        // ctx.lineWidth = 3;

        // 画第1条线
        // 从(100,100)到(300,300)画一条宽为3像素的绿色直线
        this.ctx.strokeColor = Color.YELLOW;
        this.ctx.moveTo(100, 100);
        this.ctx.lineTo(300, 300);
        this.ctx.stroke();

        // 清除历史的线, 若执行clear方法，则第1条线会被清除
        // ctx.clear();

        // 画第2条线
        this.ctx.strokeColor = Color.GREEN;
        this.ctx.moveTo(0, 0);
        this.ctx.lineTo(50, 400);
        this.ctx.stroke();

        // this.aaaawwwww()
        
    }

    update(deltaTime: number) {

    }

    public drawLines(startPos: Vec2, tagerPos: Vec2) {

        const tran: UITransform = this.getComponent(UITransform)
        const pos = tran.convertToNodeSpaceAR(new Vec3(startPos.x, startPos.y))
        const tpos = tran.convertToNodeSpaceAR(new Vec3(tagerPos.x, tagerPos.y))

        this.ctx.strokeColor = Color.WHITE;
        this.ctx.moveTo(pos.x, pos.y);
        this.ctx.lineTo(tpos.x, tpos.y);

        // log("startPos",pos)
        // log("tagerPos",tpos)

        // this.ctx.moveTo(startPos.x, startPos.y);
        // this.ctx.lineTo(tagerPos.x, tagerPos.y);
        this.ctx.stroke();
    }

    public cleat() {
        this.ctx.clear();
    }

    //测试抛物线看看
    testPP(){
         // 设置抛物线参数
        //  const startPos = new Vec3(-5, 0, 0); // 起始位置
         const startPos = this.bow.getPosition() // 起始位置
         const speed = 70; // 初速度
         const angle = 45; // 抛射角度（以度为单位）
 
         // 将角度转换为弧度
         const radian = this.degreesToRadians(angle);
 
         // 计算水平速度和垂直速度
         const horizontalSpeed = speed * Math.cos(radian);
         const verticalSpeed = speed * Math.sin(radian);
 
         // 计算抛物线总时间
         const totalTime = (2 * verticalSpeed) / 9.8;
 
         // 计算抛物线轨迹点
         const numPoints = 3; // 轨迹点数量
         const timeInterval = totalTime / numPoints; // 时间间隔
         const points = [];
         for (let i = 0; i <= numPoints; i++) {
             const t = i * timeInterval;
             const x = startPos.x + horizontalSpeed * t;
             const y = startPos.y + verticalSpeed * t - 0.5 * 9.8 * t * t;
             const point = new Vec3(x, y, 0);
             points.push(point);
         }
 
         // 创建抛物线节点，并将轨迹点设置为抛物线的路径
         const node = new Node();
         const draw = node.addComponent(Graphics);
         draw.strokeColor = Color.RED;
         draw.lineWidth = 10;
         draw.moveTo(startPos.x, startPos.y);
         for (const point of points) {
             console.log(point);
             draw.circle(point.x, point.y, 5); // 使用圆形绘制离散点
            //  draw.lineTo(point.x, point.y);
         }
         draw.stroke();
 
         // 将抛物线节点添加到场景中
         this.node.parent.addChild(node);
    }

    waniu(){
        // 设置抛物线参数
        // const startPos = new Vec3(-5, 0, 0); // 起始位置
        const startPos = this.bow.getPosition() // 起始位置
        const speed = 130; // 初速度
        const angle = 20; // 抛射角度（以度为单位）
        let targetPosition: Vec3 = new Vec3(200, 0, 0);
        const numPoints: number = 7;  //轨迹点数量
        const pointSize: number = 5; //轨迹点大小

        // 将角度转换为弧度
        const radian = this.degreesToRadians(angle);

        // 计算起始位置和目标位置之间的向量差
        // const startPos = this.node.position.clone();
        const direction = targetPosition.subtract(startPos);

        // 计算水平速度和垂直速度
        const horizontalSpeed = speed * Math.cos(radian);
        const verticalSpeed = speed * Math.sin(radian);

        // 计算抛物线轨迹点
        const timeInterval = direction.x / horizontalSpeed;
        const points: Vec3[] = [];
        for (let i = 0; i <= numPoints; i++) {
            const t = i * timeInterval / numPoints;
            const x = startPos.x + horizontalSpeed * t;
            const y = startPos.y + verticalSpeed * t - 0.5 * 9.8 * t * t;
            const point = new Vec3(x, y, 0);
            points.push(point);
        }

        // 创建抛物线节点，并将轨迹点设置为抛物线的路径
        const node = new Node();
        const draw = node.addComponent(Graphics);
        draw.strokeColor = Color.RED;
        draw.lineWidth = 10;
        for (let i = points.length - 1; i >= 0; i--) {
            const point = points[i];
            draw.circle(point.x, point.y, (points.length <= numPoints?points.length:numPoints) -i); // 使用圆形绘制离散点
        }
        draw.stroke();

        // 将抛物线节点添加到场景中
        this.node.addChild(node);
    }


    zailai(){
        const startPos = this.bow.getPosition() // 起始位置
        const force: number = 30;
        const angle = 45; // 抛射角度（以度为单位）
        const numPoints: number = 20;  //轨迹点数量
        const pointSize: number = 10; //轨迹点大小
        const maxPointSize: number = 20;
        const minPointSize: number = 5;

        // 将角度转换为弧度
        const radian = this.degreesToRadians(angle);

        // 计算水平速度和垂直速度
        const horizontalSpeed = force * Math.cos(radian);
        const verticalSpeed = force * Math.sin(radian);

        // 计算抛物线轨迹点
        const maxDistance = (2 * verticalSpeed * horizontalSpeed) / 9.8; // 最大水平距离
        const timeInterval = maxDistance / numPoints; // 时间间隔
        const points: Vec3[] = [];
        for (let i = 0; i <= numPoints; i++) {
            const t = i * timeInterval;
            const x = startPos.x + horizontalSpeed * t;
            const y = startPos.y + verticalSpeed * t - 0.5 * 9.8 * t * t;
            const point = new Vec3(x, y, 0);
            points.push(point);
        }

        // 创建抛物线节点，并将轨迹点设置为抛物线的路径
        const node = new Node();
        const draw = node.addComponent(Graphics);
        draw.strokeColor = Color.RED;
        draw.lineWidth = 15;
        for (let i = points.length - 1; i >= 0; i--) {
            const point = points[i];
            const t = i / numPoints; // 根据索引计算归一化时间
            const size = minPointSize + (minPointSize - maxPointSize) * t; // 根据归一化时间计算点的大小
            draw.circle(point.x, point.y, size); // 使用圆形绘制离散点
        }
        draw.stroke();

        // 将抛物线节点添加到场景中
        this.node.addChild(node);
    
    }

    

    // 将角度从度数转换为弧度
    degreesToRadians(degrees) {
        return degrees * (Math.PI / 180);
    }




    // private onTouchStart(pos: Vec2) {
    //     const location = this.bow.parent.convertToNodeSpaceAR(pos);

    //     const s = location.x - this.getStartPos().x;
    //     const h = location.y - this.getStartPos().y;

    //     // a*t^2 + b*t + c = 0
    //     const a = G * this.rigidBody_arrow.gravityScale * s / (2 * V * V);
    //     const b = 1;
    //     const c = a - h / s;
    //     const delta = b * b - 4 * a * c;
    //     if (delta >= 0) {
    //         // 一元二次方程求根公式
    //         const t1 = (-b + Math.sqrt(delta)) / (2 * a); // 平抛 tan 值
    //         // const t2 = (-b - Math.sqrt(delta)) / (2 * a); // 高抛 tan 值

    //         // 二、三象限角度要加 180
    //         const alpha1 = Math.atan(t1) + (s < 0 ? Math.PI : 0);
    //         // const alpha2 = Math.atan(t2) + (s < 0 ? Math.PI : 0);

    //         const v_x_1 = Math.cos(alpha1) * V;
    //         const v_y_1 = Math.sin(alpha1) * V;
    //         // const v_x_2 = Math.cos(alpha2) * V;
    //         // const v_y_2 = Math.sin(alpha2) * V;

    //         this._linearVelocity_1.x = v_x_1;
    //         this._linearVelocity_1.y = v_y_1;

    //         // this._linearVelocity_2.x = v_x_2;
    //         // this._linearVelocity_2.y = v_y_2;
    //     } else {
    //         this._linearVelocity_1 = cc.Vec2.ZERO;
    //         this._linearVelocity_2 = cc.Vec2.ZERO;
    //     }

    //     // this.drawArrowTrace();
    // }

    // // 画轨迹
    // private drawArrowTrace() {
    //     this.graphic_line.clear();
    //     // const linearVelocity = this.getArrowFirelinearVelocity();
    //     const linearVelocity = this.getArrowFirelinearVelocity();

    //     if (linearVelocity.x) {
    //         const dt = 0.05;
    //         for (let count = 0; count < 100; count++) {
    //             const time = dt * count;
    //             // s = v_x * t
    //             const dx = linearVelocity.x * time;
    //             // h = v_y * t + 0.5 * a * t * t
    //             const dy = linearVelocity.y * time + 0.5 * G * this.rigidBody_arrow.gravityScale * time * time;
    //             // 当前时间点坐标
    //             const targetX = this.getStartPos().x + dx;
    //             const targetY = this.getStartPos().y + dy;
    //             // 坐标超过地板就不画了
    //             if (targetY < -300) break;
    //             this.graphic_line.circle(targetX, targetY, 8);
    //         }
    //     }
    //     this.graphic_line.fill();
    // }

    // // 获取发射速度
    // private getArrowFirelinearVelocity() {
    //     return this.toggle_arrow.isChecked ? this._linearVelocity_2.clone() : this._linearVelocity_1.clone();
    // }

    //获取开始位置
    // getStartPos():Vec2{
    //     if(this.nodeWeapon){
    //         return this.nodeWeapon.getPosition()
    //     }
    //     return START_POS
    // }
  
}


